﻿using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using Prism.Services.Dialogs;
using System.Configuration;
using System.Data;
using System.Windows;
using WPF.Bill;
using WPF.Core;
using WPF.Core.Services;
using WPF.NoteBook;
using WPF.ToDo;
using WPF.Zero.Services;
using WPF.Zero.ViewModels;
using WPF.Zero.Views;

namespace WPF.Zero
{
    public partial class App : PrismApplication
    {
        protected override Window CreateShell() => null;

        protected override void OnInitialized()
        {
            var dialog = Container.Resolve<IDialogService>();

            dialog.ShowDialog("HelloWorldView", callBack =>
            {
                dialog.ShowDialog("LoginView", callBack =>
                { 
                    if (callBack.Result == ButtonResult.No)
                        Environment.Exit(0);
                });
            });

            var container = ContainerLocator.Container;
            var shell = container.Resolve<object>("MainView");
            if (shell is Window view)
            {
                var regionManager = container.Resolve<IRegionManager>();
                RegionManager.SetRegionManager(view, regionManager);
                RegionManager.UpdateRegions();

                if (view.DataContext is INavigationAware navigationAware)
                {
                    navigationAware.OnNavigatedTo(null);
                    App.Current.MainWindow = (Window)shell;
                }
            }

            base.OnInitialized();
        }

        protected override void RegisterTypes(IContainerRegistry services)
        {
            services.RegisterSingleton<IAppMapper, AppMapper>();

            services.RegisterDialog<HelloWorldView, HelloViewModel>();
            services.RegisterForNavigation<MainView, MainViewModel>();
            services.RegisterForNavigation<SettingView, SettingViewModel>();
            services.RegisterForNavigation<DashboardView, DashboardViewModel>();

            //数据库服务
            services.Register<BillService>();
            services.Register<IToDoService, ToDoService>();
            services.Register<INoteBookService, NoteBookService>();
            services.Register<IBillService, BillService>();
            services.Register<NoteBookService>();
            services.Register<UserService>();

            services.RegisterDialog<MsgView, MsgViewModel>();

            //基于MD组建的弹窗服务
            services.Register<IHostDialogService, HostDialogService>();
            //登录弹窗
            services.RegisterDialog<LoginView, LoginViewModel>();
            //用户注册
            services.RegisterDialog<RegisterView, RegisterViewModel>();
            services.RegisterSingleton<INavigationMenuService, NavigationMenuService>();
            services.Register<IDialogWindow, DefaultDialogWindow>(); //default dialog host
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<BillModule>(); //账单模块
            moduleCatalog.AddModule<ToDoModule>(); //待办模块
            moduleCatalog.AddModule<NoteBookModule>(); //记事本模块
            base.ConfigureModuleCatalog(moduleCatalog);
        }
    }
}
